﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using DocumentsTagger.DM;
using System.ComponentModel;

namespace DocumentsTagger.UI.Model
{
    /// <summary>
    /// This class is a Model for UI puporses that will hold the UI part that shows the document in the 
    /// selection pane.
    /// </summary>
    public class DocumentUIModel : DependencyObject, IComparable
    {
        #region members
        private Document m_Document;
        
        #endregion

        #region properties
        /// <summary>
        /// Gets or sets the file name
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// Is the underlying object was changed.
        /// </summary>
        public Boolean IsChanged
        {
            get { return (Boolean)GetValue(IsChangedProperty); }
            set { SetValue(IsChangedProperty, value); }
        }
        
        /// <summary>
        /// Gets the underlying document.
        /// </summary>
        public Document Document { get { return m_Document; } }

        // Using a DependencyProperty as the backing store for IsChanged.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsChangedProperty =
            DependencyProperty.Register("IsChanged", typeof(Boolean), typeof(DocumentUIModel), new UIPropertyMetadata(false));
        #endregion 

        #region ctor
        /// <summary>
        /// Create UI vm for showing document details in the document selection pane
        /// </summary>
        /// <param name="doc">The underlying document</param>
        public DocumentUIModel(Document doc)
        {
            this.m_Document = doc;
            this.FileName = doc.FileName;
            // connect the UI part for changes in the underlying object
            doc.Changed += new ChangeHandler(Document_Changed);
        }
        #endregion

        #region methods
        /// <summary>
        /// this function is called when the underlying object was changed.
        /// we use the Dispatcher mechanizm to make sure we make the UI changes in the right 
        /// thread.
        /// </summary>
        private void Document_Changed()
        {
            Action act = () => { this.IsChanged = true; };
            this.Dispatcher.Invoke(act, null);
        }
        #endregion

        public int CompareTo(object obj)
        {
            DocumentUIModel other = (DocumentUIModel) obj;
            return this.FileName.CompareTo(other.FileName);
        }

        

        

    }
}
